package com.taptech.doufu.util

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.content.ContentResolver
import androidx.annotation.DrawableRes
import com.taptech.doufu.constant.Constant

class ShareUtil {

    companion object {
        @JvmOverloads
        fun shareText(context: Context,title:String="豆花",desc:String = "") {
            var formatStr = Constant.changeUrlHost(desc)
            val textIntent = Intent(Intent.ACTION_SEND)
            textIntent.type = "text/plain"
            textIntent.putExtra(Intent.EXTRA_SUBJECT,"$title")  //主题
            textIntent.putExtra(Intent.EXTRA_TEXT, formatStr)
            context.startActivity(Intent.createChooser(textIntent, "分享"))
        }

        fun shareImage(context: Context,source:Int) {
            val path = getResourcesUri(context, source)
            val imageIntent = Intent(Intent.ACTION_SEND)
            imageIntent.type = "image/*"
            imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path))
            imageIntent.putExtra(Intent.EXTRA_TEXT, "我正在@Lovenovelapp 看《作品名》小说，99%女生都在用的耽美言情BL同人小说阅读器，邀请你跟我一起看（链接：https://api.lovenovelapp.com） ")

            context.startActivity(Intent.createChooser(imageIntent, "分享"))
        }


        private fun getResourcesUri(context: Context,@DrawableRes id: Int): String {
            val resources = context.resources
            val uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                    resources.getResourcePackageName(id) + "/" +
                    resources.getResourceTypeName(id) + "/" +
                    resources.getResourceEntryName(id)
            return uriPath
        }

    }

}